/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.animation
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class AnimationItem extends EventDispatcher
	{
		private var item:Sprite;
		private var effect_name:String;
		private var effect_from:Number;
		private var effect_to:Number;
		private var effect_step:Number;
		public var isAnimated:Boolean;
		
		public function AnimationItem(item:Sprite, effect_name:String, effect_from:Number, effect_to:Number, effect_step:Number)
		{
			this.item = item;
			this.effect_name = effect_name;
			this.effect_from = effect_from;
			this.effect_to = effect_to;	
			this.effect_step = effect_step;
			
			
			isAnimated = check();			
		}
		
		public function complete():void{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		public function getItemSprite():Sprite{
			return item;
		}
		
		private function check():Boolean{
			if((effect_step != 0)&&(effect_from != effect_to)){
				if(item && effect_name){
					if(item.hasOwnProperty(effect_name)){					
						try{
							item[effect_name] = effect_from;							
						}catch(error:Error){
							return false;
						}
						return true;
					}
				}
			}
			return false;	
		}
		
		public function draw():Boolean{
			var result:Boolean = false;
			if(isAnimated){
				result = true;
				
				item[effect_name] += effect_step;
			
				if(effect_from < effect_to){
					if(item[effect_name] >= effect_to){
						result = false;
					}
				}
				
				if(effect_from > effect_to){
					if(item[effect_name] <= effect_to){
						result = false;
					}
				}
			}		
			return result;
		}
	}
}